﻿#include "ThreadCache.h"
#include "ConcurrentAlloc.h"

// 测试线程局部存储
void TestTLS()
{
	std::thread t1([]
		{
			for (int i = 0; i < 4; i++)
			{
				void* ptr = ConcurrentAlloc(4);
			}
		});

	std::thread t2([]
		{
			for (int i = 0; i < 4; i++)
			{
				void* ptr = ConcurrentAlloc(4);
			}
		});

	t1.join();
	t2.join();
}

// 测试申请内存
void TestAllocte()
{
	void* ptr1 = ConcurrentAlloc(3);
	void* ptr2 = ConcurrentAlloc(4);
	void* ptr3 = ConcurrentAlloc(5);
	void* ptr4 = ConcurrentAlloc(6);
	void* ptr5 = ConcurrentAlloc(7);
	void* ptr6 = ConcurrentAlloc(8);

	std::cout << ptr1 << std::endl;
	std::cout << ptr2 << std::endl;
	std::cout << ptr3 << std::endl;
	std::cout << ptr4 << std::endl;
	std::cout << ptr5 << std::endl;
	std::cout << ptr6 << std::endl;

	for (size_t i = 0; i < 10; i++)
	{
		void* ptr = ConcurrentAlloc(i);
		std::cout << ptr << std::endl;
	}

}

// 测试释放内存
void TestDealloc()
{
	void* ptr1 = ConcurrentAlloc(3);
	void* ptr2 = ConcurrentAlloc(4);
	void* ptr3 = ConcurrentAlloc(8);
	void* ptr4 = ConcurrentAlloc(6);
	void* ptr5 = ConcurrentAlloc(7);
	void* ptr6 = ConcurrentAlloc(8);
	void* ptr7 = ConcurrentAlloc(8);

	ConcurrentFree(ptr1);
	ConcurrentFree(ptr2);
	ConcurrentFree(ptr3);
	ConcurrentFree(ptr4);
	ConcurrentFree(ptr5);
	ConcurrentFree(ptr6);
	ConcurrentFree(ptr7);
}

// 测试多块内存申请
void TestMutilBlock()
{
	std::vector<void*> vPtr;
	void* ptr = ConcurrentAlloc(1024 * 256);
	ConcurrentFree(ptr);
	for (int i = 1; i <= 1024 * 8; i++)
	{
		vPtr.push_back(ConcurrentAlloc(i));
	}

	for (int i = 1024 * 8; i >= 1; i--)
	{
		void* ptr = vPtr.back();
		vPtr.pop_back();
		ConcurrentFree(ptr);
	}
}

// 测试大块内存申请
void TestBigBlock()
{
	void* ptr = ConcurrentAlloc(1024 * 129 * 8);
	ConcurrentFree(ptr);
}

//int main()
//{
//	TestTLS();
//	// TestAllocte();
//	// TestDealloc();
//	// TestMutilBlock();
//	// TestBigBlock();
//
//	return 0;
//}